import requests
from bs4 import BeautifulSoup
# url = "https://www.baidu.com"


# 数据爬取
def get_html(url):
    r = requests.get(url, headers=headers)
    r.encoding = 'utf-8'
    # print(r.text)
    return r.text


# 数据解析
def parse_data(html_doc):
    soup = BeautifulSoup(html_doc, 'html.parser')
    div_list = soup.select("body > div.main_w.clearfix > article > div.list_s2 > div.list_s2_content > div")
    for div in div_list:
        link = div.select("div.imgw > a.list_s2_item_img")[0].get("href")
        title = div.select("a.list_s2_item_info > strong.title")[0].string
        main_food = div.select("a.list_s2_item_info > span.sc")[0].string
        print(link)
        print(title)
        print(main_food)
        print("-"*30)


# 数据存储
def store_data():
    pass


if __name__ == '__main__':
    url = "https://www.meishij.net/caixi/chuancai/"
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'en-US,en;q=0.8',
        'Cache-Control': 'max-age=0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50',
        'Connection': 'keep-alive',
        'Referer': 'https://news.sina.com.cn/'
    }
    html_doc = get_html(url)
    parse_data(html_doc)

# func = lambda b, a, c: print(b, a, c) if c == 20 else func(a, b+a, c+1)
# func(0, 1, 0)

# fun = lambda a, b, c: print(a, b, c) if c == 20 else fun(b, b+a, c+1)
# fun(0, 1, 0)
